const { ccclass, property } = cc._decorator;
import loadLobby from '../Load/loadLobby';

@ccclass
export default class skillsShow extends cc.Component {

    @property(cc.Node)
    listNode: cc.Node = null;

    _SkillIcons: cc.SpriteAtlas = null;
    _SkillsJson: any = null;
    _listed: Map<string, number> = new Map<string, number>();
    listed: Map<string, number> = new Map<string, number>();
    onLoad() {
        //获取常驻节点所绑定的脚本 
        let _loadLobby = cc.director.getScene().getChildByName('loadLobby').getComponent(loadLobby);
        if (_loadLobby) {
            this._SkillIcons = _loadLobby.SkillIcons;
            this._SkillsJson = _loadLobby.SkillsJson.SkillHs;
        }
    }
    show(hit: string[], put: string[], self: string[]) {
        this.listed = new Map<string, number>();
        if (hit?.length > 0) {
            hit.forEach(element => {
                let date = this.getParam(element, this._SkillsJson.Hit);
                if (this.listed.has(date.Icon)) {
                    this.listed.set(date.Icon, this.listed.get(date.Icon) + 1);
                }
                else {
                    this.listed.set(date.Icon, 1);
                }
            });
        }
        if (put?.length) {
            put.forEach(element => {
                let date = this.getParam(element, this._SkillsJson.Put);
                if (this.listed.has(date.Icon)) {
                    this.listed.set(date.Icon, this.listed.get(date.Icon) + 1);
                }
                else {
                    this.listed.set(date.Icon, 1);
                }
            });
        }
        if (self.length) {
            self.forEach(element => {
                let date = this.getParam(element, this._SkillsJson.Self);
                if (this.listed.has(date.Icon)) {
                    this.listed.set(date.Icon, this.listed.get(date.Icon) + 1);
                }
                else {
                    this.listed.set(date.Icon, 1);
                }
            });
        }
        this.getSkillPage();
        this.listed.forEach((value, key) => {
            if (this._listed?.has(key)) {
                this.setNodeCount(key, value);
            }
            else {
                this.setNode(key, value);
            }
        });
        this._listed?.forEach((value, key) => {
            if (!this.listed.has(key)) {
                this.listNode.getChildByName(key).removeFromParent();
            }
        });
        this._listed = this.listed;
    }
    private getSkillPage() {
        let date: string[] = gameHelper.getInstance().skillParam.a;
        date.forEach(element => {
            if (element) {
                let param = this.getParams(element);
                if (param) {
                    if (this.listed.has(param.Icon)) {
                        this.listed.set(param.Icon, this.listed.get(param.Icon) + 1);
                    }
                    else {
                        this.listed.set(param.Icon, 1);
                    }
                }
            }
        });
    }
    private setNode(icon: string, count: number) {
        let node = new cc.Node();
        node.name = icon;
        node.width = 45;
        node.height = 45;
        let sprite = node.addComponent(cc.Sprite);
        sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
        sprite.spriteFrame = this._SkillIcons.getSpriteFrame(icon);
        if (count > 1) {
            let lableNode = new cc.Node();
            lableNode.name = "label";
            let labelN = lableNode.addComponent(cc.Label);
            labelN.fontSize = 15;
            labelN.string = "x" + count;
            lableNode.x = 12;
            lableNode.y = 12;
            node.addChild(lableNode);
        }
        this.listNode.addChild(node);
    }
    private setNodeCount(icon: string, count: number) {
        if (count > 1) {
            let node = this.listNode.getChildByName(icon);
            let lableNode = node.getChildByName("label");
            if (lableNode) {
                let labelN = lableNode.getComponent(cc.Label);
                labelN.string = "x" + count;
            }
            else {
                let lableNode = new cc.Node();
                lableNode.name = "label";
                let labelN = lableNode.addComponent(cc.Label);
                labelN.fontSize = 15;
                labelN.string = "x" + count;
                lableNode.x = 15;
                lableNode.y = 15;
                node.addChild(lableNode);
            }
        }
        else {
            let node = this.listNode.getChildByName(icon);
            let lableNode = node.getChildByName("label");
            if (lableNode)
                lableNode.removeFromParent();
        }
    }

    private getParams(id: string): any {
        for (let index = 0; index < this._SkillsJson.Hit.length; index++) {
            const element = this._SkillsJson.Hit[index];
            if (element.Id == id)
                return element;
        }
        for (let index = 0; index < this._SkillsJson.Put.length; index++) {
            const element = this._SkillsJson.Put[index];
            if (element.Id == id)
                return element;
        }
        for (let index = 0; index < this._SkillsJson.Self.length; index++) {
            const element = this._SkillsJson.Self[index];
            if (element.Id == id)
                return element;
        }
    }
    private getParam(id: string, skills): any {
        for (let index = 0; index < skills.length; index++) {
            const element = skills[index];
            if (element.Id == id)
                return element;
        }
    }
}
